package com.wtzz.police.base.aop;

import com.wtzz.police.base.entity.OperationLog;
import com.wtzz.police.base.entity.User;
import com.wtzz.police.base.repository.OperationLogRepository;
import com.wtzz.police.base.util.WebTool;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.*;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.lang.reflect.Method;

@Aspect
@Component
@Slf4j
public class SysLogAspect {

    @Autowired
    OperationLogRepository repository;

    @Pointcut("@annotation(io.swagger.annotations.ApiOperation)")
    public void pointCut() {
    }

//    @Before("pointCut()")
//    public void before(JoinPoint joinPoint) {
//        long beginTime = System.currentTimeMillis();
//        MethodSignature signature = (MethodSignature) joinPoint.getSignature();
//        Method method = signature.getMethod();
//        ApiOperation annotation = method.getAnnotation(ApiOperation.class);
//        String value = annotation.value();
//        Object[] args = joinPoint.getArgs();
//        String ip = WebTool.getRequest().getRemoteAddr();
//        String uri = WebTool.getRequest().getRequestURI();
//        String userName = WebTool.getCurrentUser() == null ? "无" : WebTool.getCurrentUser().getUsername();
//        log.info("执行操作{}之前, IP地址:{}, 请求URI:{}, 用户名:{}",value,ip,uri,userName);
//    }

//    @Around("pointCut()")
//    public Object around(ProceedingJoinPoint joinPoint) throws Throwable {
//        long beginTime = System.currentTimeMillis();
//        MethodSignature signature = (MethodSignature) joinPoint.getSignature();
//        Method method = signature.getMethod();
//        OperationLog annotation = method.getAnnotation(OperationLog.class);
//        String value = annotation.value();
//        Object[] args = joinPoint.getArgs();
//        Object object = joinPoint.proceed(args);
//        String ip = WebTool.getRequest().getRemoteAddr();
//        String uri = WebTool.getRequest().getRequestURI();
//        String userName = WebTool.getCurrentUser() == null ? "无" : WebTool.getCurrentUser().getUsername();
//        log.info("IP地址:{}, 请求URI:{}, 操作:{}, 用户名:{}",ip,uri,value,userName);
//        return object;
//    }

    @After("pointCut()")
    public void after(JoinPoint joinPoint) {
        MethodSignature signature = (MethodSignature) joinPoint.getSignature();
        Method method = signature.getMethod();
        ApiOperation annotation = method.getAnnotation(ApiOperation.class);
        String operation = annotation.value();
        Object[] args = joinPoint.getArgs();
        String ip = WebTool.getRequest().getRemoteAddr();
        String uri = WebTool.getRequest().getRequestURI();
        String userName = WebTool.getCurrentUser() == null ? "无" : WebTool.getCurrentUser().getUsername();
        log.info("执行操作{}之后, IP地址:{}, 请求URI:{}, 用户名:{}", operation,ip,uri,userName);
        OperationLog log = new OperationLog();
        log.setOperation(operation);
        log.setUrl(uri);
        log.setIp(ip);
        User u  =WebTool.getCurrentUser();
        if (u!=null){
            log.setUsername(u.getUsername());
            log.setUserId(u.getId());
        }
        // FIXME: 19/11/22 是否要开启操作日志保存
//        repository.save(log);
    }

//    @AfterReturning("pointCut()")
//    public void afterReturning(JoinPoint joinPoint) {
//        long beginTime = System.currentTimeMillis();
//        MethodSignature signature = (MethodSignature) joinPoint.getSignature();
//        Method method = signature.getMethod();
//        OperationLog annotation = method.getAnnotation(OperationLog.class);
//        String value = annotation.value();
//        Object[] args = joinPoint.getArgs();
//        boolean haveUser = WebTool.getCurrentUser() != null;
//        log.info("执行{}操作返回后, 是否有用户信息{}", value, haveUser);
//    }

//    @AfterThrowing("pointCut()")
//    public void afterThrowing(JoinPoint joinPoint) {
//        long beginTime = System.currentTimeMillis();
//        MethodSignature signature = (MethodSignature) joinPoint.getSignature();
//        Method method = signature.getMethod();
//        OperationLog annotation = method.getAnnotation(OperationLog.class);
//        String value = annotation.value();
//        Object[] args = joinPoint.getArgs();
//
//    }
}